package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaDto;
import com.ysd.lis.dto.sta.StaParam;
import com.ysd.lis.entity.sta.StaInfectSite;
import com.ysd.lis.mapper.sta.StaInfectSiteMapper;
import com.ysd.lis.service.sta.StaInfectSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.sys_module 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-12-27
 */
@Service
public class StaInfectSiteServiceImpl extends ServiceImpl<StaInfectSiteMapper, StaInfectSite> implements StaInfectSiteService {

    @Autowired
    StaInfectSiteMapper staInfectSiteMapper;

    @Override
    public Result findStaInfectSiteList(StaParam pageParam) {
        MPJLambdaWrapper<StaInfectSite> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaInfectSite::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaInfectSite::getInfectSiteNo, pageParam.getSeachValue())
                    .or().like(StaInfectSite::getInfectSiteNa, pageParam.getSeachValue())
                    .or().like(StaInfectSite::getInputCode, pageParam.getSeachValue())
                    .or().like(StaInfectSite::getStandardNa,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(StaInfectSite::getCreateTime);
        List<StaInfectSite> supplierPage = staInfectSiteMapper.selectList( lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }

    @Override
    public Result addStaInfectSite(StaInfectSite staInfectSite) {
        int insert = staInfectSiteMapper.insert(staInfectSite);
        if(insert>0){
            return Result.succ(1,"添加成功",staInfectSite);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editStaInfectSite(StaInfectSite supplier) {
        int updateById = staInfectSiteMapper.updateById(supplier);
        if(updateById>0){
            return Result.succ(1,"提交成功",supplier);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result batchDeleteStaInfectSiteList(StaDto dto) {
        List<StaInfectSite> supplierList = dto.getStaInfectSiteList();
        List<String> ids = supplierList.stream().map(StaInfectSite::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }


    @Override
    public Result findStaInfectSitePageList(StaParam pageParam) {
        Page<StaInfectSite> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        MPJLambdaWrapper<StaInfectSite> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(StaInfectSite::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(StaInfectSite::getInfectSiteNo, pageParam.getSeachValue())
                    .or().like(StaInfectSite::getInfectSiteNa, pageParam.getSeachValue())
                    .or().like(StaInfectSite::getInputCode, pageParam.getSeachValue())
                    .or().like(StaInfectSite::getStandardNa,pageParam.getSeachValue()));
        }

        lambdaQueryWrapper.orderByAsc(StaInfectSite::getCreateTime);
        Page<StaInfectSite> supplierPage = staInfectSiteMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", supplierPage);
    }
}
